package com.esunny.resourcevisualization.util;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class FileUtil {

    public static List<File> getFileListByFolder(String folderPath){
        File root = new File(folderPath);
        List<File> files = new ArrayList<>();

        if (!root.isDirectory()){
            files.add(root);
        }else{
            File[] subFiles = root.listFiles();
            for (File file: subFiles) {
                files.addAll(getFileListByFolder(file.getAbsolutePath()));
            }
        }
        return files;
    }

    public static boolean delFile(File file){
        if (!file.exists()) {
            return false;
        }

        if (file.isDirectory()) {
            File[] files = file.listFiles();
            for (File f : files) {
                delFile(f);
            }
        }
        return file.delete();
    }

    public static void copyFileFromSourceToDestination(File sourceFile,File destinationFile) throws IOException {
        FileInputStream in = new FileInputStream(sourceFile);
        FileOutputStream out = new FileOutputStream(destinationFile);

        byte[] buffer = new byte[2097152];
        int readByte = 0;
        while ((readByte = in.read(buffer)) != -1){
            out.write(buffer,0,readByte);
        }

        in.close();
        out.close();
    }


}
